/**   
 * @Title: MainActivityHealthNewsFragmentMentalFragment.java 
 * @Package cn.edu.scu.healthclient.activity.fragment 
 * @Description: TODO 
 * @author 
 * @date 
 * @version V0.0   
 */
package cn.edu.cdtu.healthclient.activity.fragment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import cn.edu.cdtu.healthclient.activity.MainActivity;
import cn.edu.cdtu.healthclient.activity.NewsDetailActivity;
import cn.edu.cdtu.healthclient.connect.ConnectForHealthNews;
import cn.edu.cdtu.healthclient.domain.HealthNewsJson;
import cn.edu.scu.healthclient.activity.R;

/** 
 * @ClassName: MainActivityHealthNewsFragmentMentalFragment 
 * @Description: TODO
 * @author 
 * @date 
 *  
 */
public class MainActivityHealthNewsFragmentMentalFragment extends Fragment {

	private View mainView;
	private ListView mentalNewsListView;
	int imageIds[]=new int[]
	{
		R.drawable.m1,R.drawable.m2,R.drawable.m3,
		R.drawable.m4,R.drawable.m5,R.drawable.m6,
		R.drawable.m7,R.drawable.m8,R.drawable.m9,
		R.drawable.m10,
	};
	
	String titles[]=new String[]
	{
		"消极情绪没我们所想的那么糟",
		"春季发怒 ,最好别超三分钟",
		"不要小瞧内向孩子",
		"脾气变坏小心系疾病作怪",
		"抗击抑郁从良好生活方式开始",
		"积极心理学的负面反馈",
		"担忧就是变相的“诅咒”",
		"六大坏情绪伤五脏",
		"保持乐观只是成功的一半",
		"经常生气对健康的八个危害"
	};
	
	String describes[]=new String[]
	{
		"就我而言，我所理解的人生四大首要目标其实很多人都知道，它们分别是",
		"所谓“病从气上得，气在病中走”，生气发火危害多多，可在这本该朝气蓬勃的春季",
		"我们生活的周围仍然有很多人对内向气质的孩子充满偏见和焦虑。",
		"血糖不稳，喜怒无常。血糖升高会引起机体代谢功能紊乱，影响B族维生素的吸收",
		"临床抑郁症非常复杂：伤心及/或对日常活动不感兴趣，",
		"我最近有幸参加了两个主题演讲，每个演讲包含积极心理学的一个不同方面。",
		"现代社会，让人担忧的事太多了：担忧能否考上好学校、找到好工作，能否顺利买房",
		"愉悦时心境开朗，生气时怒火中烧，焦虑时辗转反侧，悲伤时痛哭流涕.",
		"积极的思维是帮助人们克服困难，缓解伤痛，实现新目标的有效方式。",
		"常言道，人非草木，焉能无情。中医不把人的肌体机械化，"
	};

	/* (non-Javadoc)
	 * <p>Title: onCreate</p> 
	 * <p>Description: </p> 
	 * @param savedInstanceState 
	 * @see android.support.v4.app.Fragment#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		LayoutInflater inflater = getActivity().getLayoutInflater();
		mainView = inflater.inflate(
				R.layout.activity_main_healthnews_fragment_mental_fragment, 
				(ViewGroup)getActivity().findViewById(R.id.viewPager), false);
		mentalNewsListView=(ListView)mainView.findViewById(R.id.MentalNewsListView);
		List<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>();
		for(int i=0;i<imageIds.length;i++)
		{
			Map<String,Object> listItem=new HashMap<String,Object>();
			listItem.put("image",imageIds[i]);
			listItem.put("title",titles[i]);
        	listItem.put("describe", describes[i]);
        	listItems.add(listItem);
		}
		SimpleAdapter simpleAdapter=new SimpleAdapter(getActivity(),
				listItems,R.layout
				.activity_main_healthnews_fragment_mental_fragment_mentalstyle,
				new String[]{"image","describe","title"},
				new int[]{R.id.MentalNewsImage,R.id.MentalNewsDescribe,
			R.id.MentalNewsTitle}
				);
		mentalNewsListView.setAdapter(simpleAdapter);
		mentalNewsListView.setOnItemClickListener
		(
			new OnItemClickListener()
			{
				public void onItemClick(AdapterView<?> parent,View view,
						int position,long id)
				{
					final HealthNewsAsync healthNewsAsync=new HealthNewsAsync();
					final String idString=Integer.toString(position+1);
					((MainActivity)getActivity()).getTaskQueue().addTask(
							new Thread(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							healthNewsAsync.execute(idString,"mental");
						}
					}));
				}
			}
		);
	}

	/* (non-Javadoc)
	 * <p>Title: onCreateView</p> 
	 * <p>Description: </p> 
	 * @param inflater
	 * @param container
	 * @param savedInstanceState
	 * @return 
	 * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		ViewGroup viewGroup = (ViewGroup) mainView.getParent(); 
        if (viewGroup!= null) 
        { 
        	viewGroup .removeAllViewsInLayout(); 
            Log.v("ali", "移除已存在的View");
        } 
		return mainView;
	}
	
	private class HealthNewsAsync extends AsyncTask<String,Void,HealthNewsJson>
	{
		
		protected HealthNewsJson doInBackground(String ... params)
		{
			HealthNewsJson healthNewsJson = null;
			try 
			{
				healthNewsJson=ConnectForHealthNews.connect(params[0],params[1],
						((MainActivity)getActivity()).getBinder().getHttpClient());
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			}
			return healthNewsJson;
		}
		protected void onPostExecute(HealthNewsJson healthNewsJson)
		{
			Bundle bundle=new Bundle();
			bundle.putSerializable("healthNewsJson",healthNewsJson);
			Intent newsDetailIntent=new Intent(getActivity(),NewsDetailActivity.class);
			newsDetailIntent.putExtras(bundle);
			startActivity(newsDetailIntent);
		}
	}
}
